//
//  HouseInfoGrabViewModel.m
//  Jyxt
//
//  Created by 刘日 on 17/6/1.
//  Copyright © 2017年 DaLian Rental Housing Management. All rights reserved.
//

#import "HouseInfoGrabViewModel.h"
#import "OwnerInfoModel.h"
#import "UserCenterViewModel.h"
#import "NetTool.h"
#import <MJExtension.h>
#import "CommonViewModel.h"

static NSMutableArray<OwnerInfoModel *> *ownersArr;

@implementation HouseInfoGrabViewModel

+ (NSMutableArray<OwnerInfoModel *> *)owners{
    if (!ownersArr){
        ownersArr = [NSMutableArray new];
    }
    return ownersArr;
}

+ (void)applyHouseInfoWithType: (NSString *)type
                            no: (NSString *)no
                       address: (NSString *)address
                          area: (CGFloat)area
                        photos: (NSArray<UIImage *> *)photos
                        owners: (NSArray<OwnerInfoModel *> *)owners
                   whenSuccess: (void (^)())success
                        failed: (void (^)(NSString *reason))failed{
    NSMutableDictionary *photosList = [NSMutableDictionary new];
    for (UIImage *img in photos)
        photosList[[NSString stringWithFormat: @"photo-%lu" , img.hash]] = img;
    NSMutableArray *owArr = [NSMutableArray new];
    for (OwnerInfoModel *model in owners)
        [owArr addObject: model.mj_keyValues];
    NSString *parameters = @{@"propertyIdType" : type , @"propertyIdNo" : no , @"houseAddr" : address , @"buildingArea" : @(area) , @"ownerList" : owArr}.mj_JSONString;
    [NetTool uploadWithUrl: APIURL(API_HOUSE_INFO_UPLOAD) parameters: @{@"utoken" : [UserCenterViewModel getLocalUToken] , @"data" : parameters} data: photosList onSuccess:^(NSDictionary *response) {
        [CommonViewModel checkResponseBody: response whenSuccess:^{
            success();
        } failed:^(NSString *reason) {
            failed(reason);
        }];
    } onError:^(NSError *error) {
        failed(@"无法连接到服务器");
    }];
}

@end
